﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;

[CustomEditor(typeof(UnitMeshPointComponent))]
[ExecuteInEditMode]
public class UnitMeshPointComponentEditor : Editor
{
    //在这里方法中就可以绘制面板。
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        //得到Test对象
        UnitMeshPointComponent editor = (UnitMeshPointComponent)target;

        if (GUILayout.Toggle(editor.IsShow, "IsShow"))
        {
            editor.IsShow = true;
        }
        else
        {
            editor.IsShow = false;
        }

        MainSkeletonComponent skeletonPoint = GameObject.FindObjectOfType<MainSkeletonComponent>();
        if (skeletonPoint)
        {
            List<Transform> allTransform;
            string[] allTransformName = GetAllTransform(skeletonPoint.transform, out allTransform).ToArray();

            int oldId = editor.m_selectTransform;
            //string[] alsex = { "a6/222", "a6/444", "a5", "a5", "a4", "a3", "a2" };
            oldId = EditorGUILayout.Popup("Transform", editor.m_selectTransform, allTransformName);
            if (oldId != editor.m_selectTransform)
            {
                editor.m_selectTransform = oldId;
                if (editor.m_selectTransform >= 0)
                {
                    editor.SetTransform(allTransform[editor.m_selectTransform]);
                    editor.m_TransformFindName = allTransformName[editor.m_selectTransform];
                }
            }
        }

        if (GUILayout.Button("ClearTransform"))
        {
            editor.ClearTransform();
        }
    }

    //public static List<string> GetAllTransform(Transform mainTransform, out List<Transform> allTransform)
    //{
    //    List<string> allTransformName = new List<string>();
    //    allTransform = new List<Transform>();

    //    allTransformName.Add(mainTransform.name);
    //    allTransform.Add(mainTransform);

    //    foreach (Transform item in mainTransform)
    //    {
    //        allTransformName.Add(mainTransform.name + "/" + item.name);
    //        allTransform.Add(item);
    //    }
    //    GetAllTransform(mainTransform, mainTransform.name, allTransform, allTransformName);
    //    return allTransformName;
    //}

    //private static void GetAllTransform(Transform mainTransform, string transformName, List<Transform> allTransform, List<string> allTransformName)
    //{
    //    allTransformName.Add(transformName);
    //    allTransform.Add(mainTransform);
    //    if (mainTransform.childCount > 0)
    //    {
    //        foreach (Transform item in mainTransform)
    //        {
    //            GetAllTransform(item, transformName + "/" + item.name, allTransform, allTransformName);
    //        }
    //    }
    //}


    public static List<string> GetAllTransform(Transform mainTransform, out List<Transform> allTransform)
    {
        List<string> allTransformName = new List<string>();
        allTransform = new List<Transform>();

        allTransformName.Add(mainTransform.name);
        allTransform.Add(mainTransform);

        foreach (Transform item in mainTransform)
        {
            allTransformName.Add(mainTransform.name + "/" + item.name);
            allTransform.Add(item);

            //var newItem = Copy(item, newMainTransform);
            //allTransform.Add(newItem);
            GetAllTransform(allTransform, allTransformName, item);
        }
        return allTransformName;
    }

    public static void GetAllTransform(List<Transform> allTransform, List<string> allTransformName, Transform mainTransform)
    {
        foreach (Transform item in mainTransform)
        {
            allTransform.Add(item);
            allTransformName.Add(mainTransform.name + "/" + item.name);
            if (item.childCount > 0)
            {
                GetAllTransform(allTransform, allTransformName, item);
            }
        }
    }


}
